package com.kuke.kukeproduct.controller;


import cn.hutool.core.bean.BeanUtil;
import com.kuke.kukecommon.entity.ResultList;
import com.kuke.kukecommon.entity.Result;
import com.kuke.kukecommon.redis.RedisUtil;
import com.kuke.kukeproduct.bo.ProductBO;
import com.kuke.kukeproduct.dto.ProductDTO;
import com.kuke.kukeproduct.entity.Product;
import com.kuke.kukeproduct.service.ProductService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;


/**
 *  controller
 *
 * @author kuke
 * @since 2021-08-20
 */
@RefreshScope
@Api("product")
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("get1")
    public String get(){
        return "sssss";
    }

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "productDto", value = "新增产品productDto", required = true, dataType = "ProductDTO")
    @PostMapping
    public Result add(@RequestBody ProductDTO productDto) {
        log.info("name:{}", productDto);
        Product product = BeanUtil.copyProperties(productDto,Product.class);
        return Result.success(productService.add(product));
    }


    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        return Result.success(productService.delete(id));
    }


    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "productDTO", value = "产品实体", required = true, dataType = "ProductDTO")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable Long id, @RequestBody ProductDTO productDTO) {
        Product product = BeanUtil.copyProperties(productDTO,Product.class);
        product.setId(id);
        return Result.success(productService.update(product));
    }


    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedissonClient redissonClient;

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable Long id) {
//        redisUtil.set("dd","33",30);
//
//        log.info("get with id:{}",id+(String)redisUtil.get("dd"));
//
//        //参考RedissonLockTest
//        RLock lock = redissonClient.getLock("lock"); //非公平锁,随机取一个等待中的线程分配锁
//        //RLock lock=redissonClient.getFairLock("lock"); //公平锁,按照先后顺序依次分配锁
//        try {
//            if (lock.tryLock(2, 10, TimeUnit.SECONDS)) { //最多等待锁2秒，10秒后强制解锁,推荐使用
//                System.out.println(formatDate() + " " + Thread.currentThread().getName() + "获取到锁");
//                Thread.sleep(4500);
//            } else {
//                System.out.println(formatDate() + " " + Thread.currentThread().getName() + "未获取到锁");
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } finally {
//            if (null != lock && lock.isHeldByCurrentThread()) {
//                lock.unlock();
//            }
//        }

        Object obj = productService.get(id);
        System.out.println("");
//        return Result.success(productService.get(id));
        return Result.success(ResultList.instance(new ArrayList()));
    }

    public String formatDate() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.SECOND) + ":" + c.get(Calendar.MILLISECOND);
    }


    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "产品名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result query(@RequestParam String name) {
        log.info("query with name:{}", name);
        ProductBO productBo = new ProductBO();
        productBo.setProductName(name);
        return Result.success(productService.query(productBo));
    }

    @ApiOperation(value = "搜索产品", notes = "根据条件查询产品信息")
    @ApiImplicitParam(name = "productQueryForm", value = "产品查询参数", required = true, dataType = "ProductQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/conditions")
    public Result search(@RequestBody ProductDTO productDTO) {
        log.info("search with productDTO:{}", productDTO);
        ProductBO productBO = BeanUtil.copyProperties(productDTO,ProductBO.class);
        return Result.success(productService.query(productBO));
    }

}

